﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Netflix Subtitles Database</title>
    <link rel="stylesheet" href="css/popup.css" />
    <script src="js/jquery-2.0.3.js"></script>
    <script src="js/linq.js"></script>
    <script src="js/netflix.js"></script>
    <script>
        var Popup = {
            setTitle: function (value) {
                $('#movieTitle').text(value);
            },

            getItem: function () {
                return {
                    title: $('#movieTitle').text(),
                    lang: $('#subtitleLang').val(),
                    link: $('#subtitleLink').val()
                };
            },

            add: function (title, url) {
                var link = $('<a target="_top"></a>');
                $(link).attr('href', url);
                $(link).text(title);

                $('#subtitlesList').append(link);
            },

            clear: function () {
                $('#subtitlesList').remove('a');
            },

            onLoad: function (data) {
                Popup.clear();
                Popup.setTitle(Netflix.get('t'));                

                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    Popup.add(item.title, item.link);
                }
            },

            onInserted: function () {
                $('#sendButton').removeAttr('disabled');
                $('#subtitleLink').val('');
            },

            onSend: function () {
                $('#sendButton').attr('disabled', 'disabled');
                Netflix.insert(Popup.getItem());
            },

            init: function () {
                // http://movies.netflix.com/WiPlayer?movieid=70236516&trkid=7203170&t=Superman%2Bvs.%2BThe%2BElite&tctx=-99%2C-99%2Cc6c34657-a0ae-4ecd-bc21-0616544482bd-8695985
                $('#sendButton').click(this.onSend);
                Netflix.onLoad = this.onLoad;
                Netflix.onInserted = this.onInserted;
                Netflix.load();
            }
        }

        $(document).ready(function () {
            Popup.init();
        });
    </script>
</head>
<body>
    <header>
        <h1>Netflix Subtitles Database v0.5</h1>
    </header>

    <section>
        <div>
            <label style="font-weight: bold">Movie:</label>
            <span id="movieTitle"></span>
        </div>
        <div id="subtitlesList"></div>
    </section>
    <hr />
    <footer>
        <div>
            <label>Language:</label>
            <select id="subtitleLang">
                <option selected="selected">pt-BR</option>
            </select>
        </div>
        <div>
            <label>Link:</label>
            <input type="url" id="subtitleLink" size="32"/>
        </div>
        <input type="button" id="sendButton" value="Send" style="width: 100%" />
    </footer>
</body>
</html>
